<?php 
session_start();
ob_start();
include("connection/connection.php");
$form_id=$_POST['frmId'];
$qryGetData="select * from tbl_dtls_q1_main where id=$form_id";
$recGetData=mysql_query($qryGetData);
$rowGetData=mysql_fetch_array($recGetData);
$districtMain=$rowGetData['district_id'];
$district_rwss=explode(",",$districtMain);

$villageMain=$rowGetData['village_id'];
$village_rwss=explode(",",$villageMain);

$census=$rowGetData['cvc'];
$census_rwss=explode(",",$census);

$blockMain=$rowGetData['block_id'];
$block_rwss=explode(",",$blockMain);

$panchayatMain=$rowGetData['panchayat_id'];
$panchayat_rwss=explode(",",$panchayatMain);

$popBenifitedMain=$rowGetData['population_benefited'];
$popPresentMain=$rowGetData['present_population'];
$district=$_POST['district'];
if(in_array($district,$district_rwss,true))
{
	$district=$districtMain;
	
}
else
{
	$district=$districtMain.",".$district;
	
}

$block=$_POST['block'];
if (in_array($block,$block_rwss,true)) 
{
    $block=$blockMain;
}
else
{
	$block=$blockMain.",".$block;
}
$panchayat=$_POST['panchayat'];
if (in_array($panchayat,$panchayat_rwss,true)) 
{
    $panchayat=$panchayatMain;
}
else
{
	$panchayat=$panchayatMain.",".$panchayat;
}
$village=$_POST['village'];
if(in_array($village,$village_rwss,true))
{
	$village=$villageMain;
}
else
{
	$village=$villageMain.",".$village;
}

$cvc=$_POST['cvc'];
if(in_array($cvc,$census_rwss,true))
{
	$cvc=$census;
}
else
{
	$cvc=$census.",".$cvc;
}

$q2="SELECT DISTINCT habitation_code, habitation_name,total_population from tbl_lookup_location where village_id=$_POST[village] order by habitation_code";
$r2=mysql_query($q2);
$num_rows = mysql_num_rows($r2);
$tot_pop=0;
for($i=1; $i<=$num_rows; $i++)
{
	$hname = 'name_habi'.$i;
	$hcode = 'habi_code'.$i;
	$type_habit='type_habit'.$i;
	$status = 'status'.$i;
	$cover='cover'.$i;
	$pop='pop'.$i;
	$hname=$_POST[$hname];	
	$hcode=$_POST[$hcode];
	$type_habit=$_POST[$type_habit];
	$status=$_POST[$status];
	$cover=$_POST[$cover];
	if($cover=="Covered")
	{
		$total_pop=$total_pop + $_POST[$pop];
	}
	$q4="INSERT INTO tbl_dtls_q1_oh(loc_id, habit_name, habit_code, type_habit, status_e_nt) VALUES ('$form_id', '$hname', '$hcode', '$type_habit', '$status')";
	$r4=mysql_query($q4);
	
$q_crit_vill="INSERT INTO tbl_dtls_q1_critical(loc_id, critical_hname, critical_hcode, type_habit, critical) VALUES ('$form_id', '$hname', '$hcode', '$type_habit', 'no')";
$r_crit_vill=mysql_query($q_crit_vill);	

$q_tanker_vill="INSERT INTO tbl_dtls_q1_tanker(loc_id, habitation_code, habitation_name, type_habit, total_tanker_no, water_quantity, area_covered, to_date, from_date) VALUES ('$form_id', '$hcode', '$hname', '$type_habit', '', '', '', '', '')";
$r_tanker_vill=mysql_query($q_tanker_vill);
	
}

$popBenifitedMain=$popBenifitedMain + $total_pop;
$qryPopFactor="select * from tbl_lookup_pop_factor where district_id='$district'";
$recPopFactor=mysql_query($qryPopFactor);
$rowPopFactor=mysql_fetch_array($recPopFactor);
$factor=$rowPopFactor['population_factor']; 
$popPresentMain=round($popBenifitedMain * $factor); 
$qryVillage="UPDATE tbl_dtls_q1_main SET district_id='$district',block_id='$block',panchayat_id='$panchayat',village_id='$village',cvc='$cvc',population_benefited='$popBenifitedMain',present_population='$popPresentMain' where id='$form_id'";
$recVillage=mysql_query($qryVillage);
	

if($r4 && $recVillage)
{
	header('Location: q1_rwss_edit.php');
	//echo "success 2";
}

?>

